#include "ObjectRenderWidget.h"
#include <QPainter>
#include <QMessageBox>
#include <QDomDocument>

ObjectRenderWidget::ObjectRenderWidget(QWidget *parent) :
    QWidget(parent)
{
    mObject = new Object;

    mBackgrounds.append(new QNinePatch(QString::fromUtf8(DEFAULT_BG_01)));
    mBackgrounds.append(new QNinePatch(QString::fromUtf8(DEFAULT_BG_02)));
    mBackgrounds.append(new QNinePatch(QString::fromUtf8(DEFAULT_BG_03)));
    mBackgrounds.append(new QNinePatch(QString::fromUtf8(DEFAULT_BG_04)));
    mBackgrounds.append(new QNinePatch(QString::fromUtf8(DEFAULT_BG_05)));
    mBackgrounds.append(new QNinePatch(QString::fromUtf8(DEFAULT_BG_06)));
    mBackgrounds.append(new QNinePatch(QString::fromUtf8(DEFAULT_BG_07)));
    mBackgrounds.append(new QNinePatch(QString::fromUtf8(DEFAULT_BG_08)));
    mBackgrounds.append(new QNinePatch(QString::fromUtf8(DEFAULT_BG_09)));

    mIcons.append(new QPixmap(QString::fromUtf8(DEFAULT_IC_01)));
    mIcons.append(new QPixmap(QString::fromUtf8(DEFAULT_IC_02)));
    mIcons.append(new QPixmap(QString::fromUtf8(DEFAULT_IC_03)));
    mIcons.append(new QPixmap(QString::fromUtf8(DEFAULT_IC_04)));
    mIcons.append(new QPixmap(QString::fromUtf8(DEFAULT_IC_05)));
    mIcons.append(new QPixmap(QString::fromUtf8(DEFAULT_IC_06)));
    mIcons.append(new QPixmap(QString::fromUtf8(DEFAULT_IC_07)));
}

ObjectRenderWidget::~ObjectRenderWidget(){
    mChilds.clear();
    mBackgrounds.clear();
    mIcons.clear();
    delete mObject;
}

void ObjectRenderWidget::clearData(){
    while(!mChilds.isEmpty()){
        QLabel* child = mChilds.takeFirst();
        delete child;
    }
}

bool ObjectRenderWidget::setData(QByteArray data){
    QDomDocument document;
    QString errorMsg;
    if(!document.setContent(data, &errorMsg)){
        QMessageBox msgBox;
        msgBox.setText(QString("Xml parsing error! : \n").append(errorMsg));
        msgBox.exec();
        return false;
    }

    QDomElement rootElement = document.documentElement();
    if(rootElement.isNull() || rootElement.tagName().compare("Object", Qt::CaseInsensitive)){
        QMessageBox msgBox;
        msgBox.setText(QString("Document is not available."));
        msgBox.exec();
        return false;
    } else {
        mObject->clear();

        QRgb bgColor = (QRgb)rootElement.attribute("bgcolor", "0").toInt();
        QString bgimg = rootElement.attribute("bgimg", "");
        int width = (QRgb)rootElement.attribute("width", "0").toInt();
        int height = (QRgb)rootElement.attribute("height", "0").toInt();
        int index = (QRgb)rootElement.attribute("index", "0").toInt();

        mObject->bgColor = bgColor;
        mObject->mSize = QSize(width, height);
        mObject->bgIndex = index;

        if(!bgimg.isEmpty()){
            mObject->bgImage = new QNinePatch(bgimg);
        }
    }

    QDomNode childNode = rootElement.firstChild();
    while(!childNode.isNull()){
        QDomElement element = childNode.toElement();
        if(!element.tagName().compare("txt", Qt::CaseInsensitive)){
            int x = element.attribute("x").toInt();
            int y = element.attribute("y").toInt();
            int w = element.attribute("width").toInt();
            int h = element.attribute("height").toInt();
            int s = element.attribute("size", "-1").toInt();
            QRgb c = (QRgb)element.attribute("color").toInt(0, 16);
            QString type = element.attribute("type");
            QString text = element.text();

            QLabel* item = new QLabel(this);
            item->setGeometry(x, y, w, h);
            item->setText(text);

            if( c > 0 ){
                QPalette palette;
                QBrush brush(QColor(qRed(c), qGreen(c), qBlue(c)));
                brush.setStyle(Qt::SolidPattern);
                palette.setBrush(QPalette::Active, QPalette::WindowText, brush);
                palette.setBrush(QPalette::Inactive, QPalette::WindowText, brush);
                item->setPalette(palette);
            }

            if(!type.isEmpty() && s > 0){
                QFont font;
                font.setBold(type.indexOf("bold") > -1);
                font.setItalic(type.indexOf("italic") > -1);
                font.setUnderline(type.indexOf("underline") > -1);
                font.setStrikeOut(type.indexOf("strikeout") > -1);
                font.setPointSize(s);
                item->setFont(font);
            } else if(type.isEmpty() && s > 0){
                QFont font;
                font.setPointSize(s);
                item->setFont(font);
            } else if(!type.isEmpty()) {
                QFont font;
                font.setBold(type.indexOf("bold") > -1);
                font.setItalic(type.indexOf("italic") > -1);
                font.setUnderline(type.indexOf("underline") > -1);
                font.setStrikeOut(type.indexOf("strikeout") > -1);
                item->setFont(font);
            }

            item->show();
            mChilds.append(item);
        } else if (!element.tagName().compare("img", Qt::CaseInsensitive)) {
            int x = element.attribute("x").toInt();
            int y = element.attribute("y").toInt();
            int w = element.attribute("width").toInt();
            int h = element.attribute("height").toInt();
            int i = element.attribute("index").toInt();
            QString src = element.attribute("src");

            QLabel* item = new QLabel(this);
            item->setGeometry(x, y, w, h);
            if(src.isEmpty())
                item->setPixmap(mIcons.at(i)->copy(mIcons.at(i)->rect()));
            else
                item->setPixmap(QPixmap(src));

            item->show();
            mChilds.append(item);
        }
        childNode = childNode.nextSibling();
    }

    update();
    int count = mChilds.size();
    for(int i = 0; i < count; i++)
        mChilds.at(i)->show();

    return true;
}

void ObjectRenderWidget::paintEvent(QPaintEvent*){
    if(mObject == 0 || mObject->mSize.isEmpty()) return;

    QPainter painter(this);
    QPen pen;
    pen.setWidth(0);
    painter.setPen(pen);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setBrush(QBrush(QColor(mObject->bgColor)));

    if(mObject->bgColor != 0)
        painter.drawRect(0, 0, mObject->mSize.width(), mObject->mSize.height());

    if(mObject->bgImage != 0 && mObject->bgImage->isNinePatch)
        painter.drawImage(0, 0, mObject->bgImage->getNinePatchPicture(mObject->mSize));
    else if(mObject->bgImage != 0 && !mObject->bgImage->isNinePatch)
        painter.drawPixmap(0, 0, mObject->mSize.width(), mObject->mSize.height(), mObject->bgImage->getOrgPicture(mObject->mSize));
    else
        painter.drawImage(0, 0, mBackgrounds.at(mObject->bgIndex)->getNinePatchPicture(mObject->mSize));
}
